package com.small.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.view.LoadingDailog;
import com.smell.R;

public class RegisterActivity extends Activity {
	LoadingDailog dialog;
	private Action action;
	String dialogMsg, toastMsg;
	EditText txt_name, txt_pass, txt_nick, txt_phone;
	Button btn_on;
	int result = 0;
	String strName = "";
	String strPass = "";
	String strNick = "";
	String strPhone = "";
	private boolean isLogin = false;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_view);
		action = new Action(RegisterActivity.this);
		onView();
	}

	public void onView() {
		txt_name = (EditText) this.findViewById(R.id.reg_txt_name);
		txt_pass = (EditText) this.findViewById(R.id.reg_txt_pass);
		txt_nick = (EditText) this.findViewById(R.id.reg_txt_nick);
		txt_phone = (EditText) this.findViewById(R.id.reg_txt_phone);
		btn_on = (Button) this.findViewById(R.id.reg_btn_on);
		btn_on.setOnClickListener(onClick);
	}

	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.reg_btn_on:
				onReg();
				break;
			}
		}
	};

	public void onReg() {
		strName = txt_name.getText().toString().trim();
		strPass = txt_pass.getText().toString().trim();
		strNick = txt_nick.getText().toString().trim();
		strPhone = txt_phone.getText().toString().trim();
		if (strName.equals("")) {
			txt_name.setError("请输入");
			return;
		}
		if (strPass.equals("")) {
			txt_pass.setError("请输入");
			return;
		}
		if (strNick.equals("")) {
			txt_nick.setError("请输入");
			return;
		}
		if (strPhone.equals("")) {
			txt_phone.setError("请输入");
			return;
		}

		action.showProgressDialog("正在注册...");
		isLogin = false;
		new AsyncDataLoader(asyncCallback).execute(); 
 
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
			if (isLogin) {
				result = action.doLogin(strName, strPass);
			} else {
				result = action.doRegister(strName, strPass, strPhone, strNick);
				 
			}
		}

		@Override
		public void onPrepare() {
		}

		@Override
		public void onFinish() {
			action.stopProgressDialog();
			
			if (isLogin) {
				doLogin(result);
			}else{
				doRegister(result);
			} 
		}
	};

	protected void doRegister(int result2) {
		switch (result2) {
		 
		case 0:
			action.showShortToast("注册失败...");
			break;
		case 1: 
			action.showShortToast("注册成功..."); 
			new AsyncDataLoader(asyncCallback).execute(); 
			isLogin = true;
			break;
		
		case 2:
			action.showShortToast("用户名已经存在...");
			break;
		case -2:
			action.showShortToast("网络异常，请稍候重试...");
			break;
		default:
			action.showShortToast("注册失败...");
			break;
		} 
	}

	protected void doLogin(int result2) {
		switch (result2) { 
		case 1: 
			action.saveUserInfo(strName, strPass,true);
			action.showShortToast("登录成功...");
			Intent i = new Intent(RegisterActivity.this, UserCentreActivity.class);
			RegisterActivity.this.startActivity(i);
			RegisterActivity.this.finish();
			break; 
		case -2:
			action.showShortToast("网络异常、登录失败...");
			break;

		default:
			action.showShortToast("注册成功,但是登录失败,请稍候重试...");
			break;
		} 
	}  
} 